package net.jsoft.pms;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import net.jsoft.Application;
import net.jsoft.platform.security.entity.user.SysUser;
import net.jsoft.platform.security.repository.UserRepository;
import net.jsoft.pms.entity.dept.Dept;
import net.jsoft.pms.entity.plan.Plan;
import net.jsoft.pms.entity.problem.ProblemTask;
import net.jsoft.pms.repository.task.TaskRepository;
import net.jsoft.pms.service.mysql.problem.IProblemService;
import net.jsoft.pms.util.ReadProjectMpp;
import net.sf.mpxj.ProjectFile;
import net.sf.mpxj.Resource;
import net.sf.mpxj.ResourceAssignment;
import net.sf.mpxj.Task;

@RunWith(SpringRunner.class) //14.版本之前用的是SpringJUnit4ClassRunner.class
@SpringBootTest(classes = Application.class)
public class ImportPMS
{
    @Autowired
    private TaskRepository repository;

    @Autowired
    private UserRepository userRepository;

    @javax.annotation.Resource(name = "problemServiceImpl")
    IProblemService service;

    @Test
    public void importPMS() throws FileNotFoundException
    {
        File f = new File("D:/tax/电子税务局/K文档类/电子税务局/1.工作库/0.项目管理/03.计划及进度管理/01.总体计划/01.电子税务局综合管理系统/电子税务局局端系统开发计划.mpp");
        InputStream inputStream = new FileInputStream(f);
        List<Plan> planTasks = new ArrayList<Plan>();
        List<Task> task = new ArrayList<Task>();
        ProjectFile project = null;

        Plan pt = null;
        try
        {
            project = ReadProjectMpp.readFile(inputStream);
            task = project.getAllTasks();
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        for (Task t : task)
        {
            if (StringUtils.isEmpty(t.getName()))
            {
                continue;
            }
            List<ResourceAssignment> l = t.getResourceAssignments();
            SysUser u = null;
            Dept dept1 = userRepository.findDept(1);
            Dept dept2 = userRepository.findDept(2);
            Dept dept3 = userRepository.findDept(3);
            Dept dept = null;

            // 拥有资源
            for (ResourceAssignment ra : l)
            {
                Resource r = ra.getResource();
                if (r != null)
                {
                    u = userRepository.findUserByFullName(r.getName());
                    if (u == null)
                    {
                        System.out.println("===================" + r.getName());
                    }

                    pt = repository.findOneByUniqueID(t.getUniqueID(), u);
                    if (pt == null)
                    {
                        System.out.println(t.getUniqueID());
                        pt = new Plan();
                        pt.setId(t.getID());
                    }

                    pt.setOptPerson(u);

                    pt.setSource(t.getOutlineCode(1));

                    switch (t.getOutlineCode(2) + "")
                    {
                    case "报税组":
                        dept = dept1;
                        break;
                    case "办税组":
                        dept = dept2;
                        break;
                    case "消息与权限组":
                        dept = dept3;
                        break;
                    default:
                        dept = dept3;
                    }

                    pt.setDept(dept);
                    pt.setType(t.getOutlineCode(3));
                    pt.setInterfaceName(t.getText(4));
                    pt.setRegion(t.getText(12));
                    pt.setName(t.getName());

                    pt.setEarlyStart(t.getEarlyStart());
                    pt.setEarlyFinish(t.getEarlyFinish());
                    pt.setLateStart(t.getActualStart());
                    pt.setLateFinish(t.getActualFinish());
                    pt.setEarlyVersion(t.getText(9));
                    pt.setVersionDate(t.getDate(1));
                    pt.setLateVersion(t.getText(10));
                    pt.setNotes(t.getNotes());

                    ProblemTask problemTask = service.findOne(t.getText(5));
                    pt.setProblem(problemTask);
                    if (t.getPercentageComplete() != null)
                    {
                        pt.setPercentageComplete(t.getPercentageComplete().doubleValue() + "");
                    }
                    else
                    {
                        pt.setPercentageComplete(0.00 + "");
                    }
                    if (pt.getLateFinish() == null)//&& d != null && t.getLateFinish().equals(d))
                    {
                        pt.setFinishFlag("未完成");
                    }
                    else
                    {
                        pt.setFinishFlag("完成");
                    }

                    pt.setCreateDate(t.getCreateDate());
                    pt.setStopDate(t.getStop());
                    pt.setUniqueId(t.getUniqueID());
                    if (t.getParentTask() != null)
                    {
                        pt.setParentId(t.getParentTask().getID());
                    }

                    planTasks.add(pt);
                }
            }
        }
        try
        {
            repository.save(planTasks);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}
